package com.neuedu.yyzx.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.yyzx.dto.UserGrantDTO;
import com.neuedu.yyzx.mapper.MenuMapper;
import com.neuedu.yyzx.mapper.UserMapper;
import com.neuedu.yyzx.mapper.UserRoleMapper;
import com.neuedu.yyzx.po.Menu;
import com.neuedu.yyzx.po.User;
import com.neuedu.yyzx.po.UserRole;
import com.neuedu.yyzx.service.UserService;
import com.neuedu.yyzx.utils.JWTUtils;
import com.neuedu.yyzx.vo.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【user(用户表)】的数据库操作Service实现
 * @createDate 2025-04-08 09:23:03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    @Transactional
    public boolean grant(UserGrantDTO dto) throws Exception {
        Long userId = dto.getUserId();
        Long[] roleIds = dto.getRoleIds();

        if (roleIds == null || roleIds.length <= 0) {
            throw new Exception("授权角色不能为空");
        }


        // 1先删除
        UpdateWrapper<UserRole> wrapper = new UpdateWrapper<>();
        wrapper.eq("user_id", userId);
        userRoleMapper.delete(wrapper);

        // 2 后重新插入
        List list = new ArrayList<>();
        for (Long roleId : roleIds) {
            list.add(new UserRole(userId, roleId));
        }

        // 批量插入
        Object result = userRoleMapper.insert(list);

        return true;
    }


    /***
     * 获取用户身份、昵称、权限菜单
     * @param token
     * @return
     */
    @Override
    public UserInfoVO info(String token) {
        UserInfoVO vo = new UserInfoVO();

        Long userId = JWTUtils.getUserId(token);

        // 用户基本信息
        vo.setUser(getBaseMapper().selectById(userId));

        // 用户授权菜单信息
        List<Menu> userGrantMenus = this.getUserGrantMenus(userId);

        // 组织 上下级关系
        vo.setGrantMenus(getUserGrantMenuTree(userGrantMenus, userId));
        return vo;
    }


    /**
     * 查询用户拥有的权限列表
     *
     * @param userId
     * @return
     */
    public List<Menu> getUserGrantMenus(long userId) {
        // 用户授权菜单信息
        List<Menu> userGrantMenus = menuMapper.getUserGrantMenus(userId);
        return userGrantMenus;
    }

    /**
     * 根据用户ID，获取所有首选菜单，以父子结构构造
     *
     * @param userId
     * @return
     */
    public List<Menu> getUserGrantMenuTree(List<Menu> userGrantMenus, Long userId) {

        List result = new ArrayList();
        for (Menu currentMenu : userGrantMenus) {
            if (currentMenu.getParentId().intValue() == 0) {
                result.add(currentMenu);
                getChildrenMenu(userGrantMenus, currentMenu);
            }
        }
        return result;
    }

    private void getChildrenMenu(List<Menu> userGrantMenus, Menu parentMenu) {
        for (Menu currentMenu : userGrantMenus) {
            if (parentMenu.getMenuId().equals(currentMenu.getParentId())) {
                parentMenu.getChildren().add(currentMenu);
                // 递归查找下级
                getChildrenMenu(userGrantMenus, currentMenu);
            }
        }
    }
}




